//
//  HJTeasingDetailVC.m
//  project
//
//  Created by jc on 2019/5/7.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJTeasingDetailVC.h"
#import "HJTeasingDetailHeaderView.h"
#import "HJSettingVC.h"

@interface HJTeasingDetailVC ()
@property (nonatomic,strong) HJTeasingDetailHeaderView * headerView;
@end

@implementation HJTeasingDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"吐个槽";
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.tableHeaderView = self.headerView;
}

-(HJTeasingDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[HJTeasingDetailHeaderView alloc]init];
        _headerView.model = self.teasingModel;
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH);
        @weakify(self)
        _headerView.commitBlock = ^(NSString *phone, NSString *content, NSArray *photos, NSString *typeId) {
            @strongify(self)
            NSMutableDictionary * params = [NSMutableDictionary dictionary];
            params[@"phone"] = phone;
            params[@"content"] = content;
            params[@"typeId"] = typeId;
            params[@"userId"]= [HJUserManager getUserModel].userId;
            [self updateDataWithParams:params images:photos];
        };
    }
    return _headerView;
}

-(void)updateDataWithParams:(NSDictionary *)params images:(NSArray *)images {
    [YFHud showProgress:@"提交中"];
    NSString *UploadImagUrl = [Api stringByAppendingString:UploadImageApi];
    @weakify(self)
    [HJHttpTool uploadImageWithUrl:UploadImagUrl params:nil name:@"multipartFiles" imageArray:images completion:^(NSArray *result, NSError *error) {
        if (error) {
            [YFHud showError:@"提交失败"];
            return ;
        }
        LKLog(@"上传图片数组成功");
        // 装配参数
        NSMutableArray *images = [NSMutableArray arrayWithCapacity:result.count];
        for (int i = 0; i < result.count; i ++) {
            NSString *imageURLString = result[i];
            NSString *fileType = @"image";
            NSDictionary *files = @{
                                    @"url":imageURLString,
                                    @"fileType": fileType,
                                    };
            [images addObject:files];
        }
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:images options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonFiles = [[NSString alloc] initWithData:jsonData encoding:(NSUTF8StringEncoding)];
        
        LKLog(@"图片参数files：%@",jsonFiles);
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        if (params != nil) {
            [parameters addEntriesFromDictionary:params];
        }
        [parameters setObject:jsonFiles forKey:@"files"];
        
        NSString *url = [Api stringByAppendingString:CommitApi];
        @strongify(self)
        @weakify(self)
        [HJHttpTool post:url params:parameters success:^(HJResponeds *result) {
            @strongify(self)
            if (result.isSuccess) {
                for (UIViewController * vc in self.navigationController.viewControllers) {
                    if ([vc isKindOfClass:NSClassFromString(@"HJSettingVC")]) {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                           [self.navigationController popToViewController:vc animated:YES];
                        });
                        break;
                    }
                }
            } else {
                [YFHud showError:result.resultDesc];
            }
        } failure:^(id result) {
            [YFHud showError:@"提交失败"];
        }];
    }];
}

@end
